#pragma GCC optimize(2)
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <vector>
#include <set>

using namespace std;
using LL = long long;   

int main(){
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    set<int> st;
    map<int, int> mp;
    int q, op, x, c;
   
    cin >> q;
    while(q --){
        cin >> op;
        if(op == 1){
            cin >> x;
            if(mp[x] == 0){
                mp[x] ++;
                st.insert(x);
            }else{
                mp[x] ++;
            }
        }else if(op == 2){
            cin >> x >> c;
            if(mp[x] <= c){
                mp[x] = 0;
                st.erase(x);
            }else{
                mp[x] = mp[x] - c;
            }
        }else{
            int minv = *st.begin();
            int maxv = *(st.rbegin());
            cout << maxv - minv << '\n';
        }

    }



    return 0;
}